<?php
class Cartridge extends BaseDB
{
	function __construct()
	{
		$this->table = 'cartridges';
		parent::__construct();
	}
	
	/**
	 * 添加硒鼓
	 *
	 * @param unknown_type $data
	 * @return unknown
	 */
	public function add($data)
	{
		if (empty($data['type']) || empty($data['inventory']))return false;
		return $this->insert($data);
	}
	
	public function delById($id)
	{
		$id = (int)$id;
		if (empty($id) || $id < 0)return false;
		return $tihs->delete(array('id'=>$id));
	}
	
	public function delByType($type)
	{
		if (empty($type))return false;
		return $this->delete(array('type'=>$type));
	}
	
	public function getList($start=0 , $count=20)
	{
		$start = max(0, (int)$start);
		$count = max(20, (int)$count);
		
		return $this->select(false, $start, $count);
	}
	
	public function modifyById($inventory, $id)
	{
		$id = (int)$id;
		if (empty($id) || $id < 0 || empty($inventory)) return false;
		return $this->update(array('inventory'=>$inventory), array('id'=>$id));
	}
	
	public function getById($id)
	{
		$id = max(0, (int)$id);
		if (empty($id)) return false;
		
		return $this->select(array('id' => $id));
	}
	
	public function getByIds($ids) 
	{
		if (empty($ids) || !is_array($ids)) return false;
		return $this->getXByIds($ids);
	}
	
	/**
	 * 查询硒鼓库存
	 *
	 * @param $where array('machine_id', 'machine_type_id')
	 * @param int $start
	 * @param int $count
	 * @return array $res
	 */
	public function queryByType($type, $start=0 , $count=20)
	{
		if (empty($type))return false;
		$start = max(0, (int)$start);
		$count = max(20, (int)$count);
		
		return $this->select(array('type' => $type), $start, $count);
	}
	
	public function queryByTypes($types) {
		$old_types = $types;
		$types = is_array($types) ? $types : array($types);
		$sql = "select * from " . $this->table . " where type in (" . $this->getSelectInRangeStr($types) . ")";
		$res = $this->db->getAll($sql, $types);
		
		if (empty($res)) {
			return array('total'=>0, 'data'=>array());
		}
		
		$ret = array();
		foreach($res as $k=>$v) {
			$ret[$v['type']] = $v;
		}
		
		if (is_array($old_types))
		{
			return array('total' => count($ret), 'data' => $ret);
		}
		else
		{
			return array('total' => 1, 'data' => $ret[0]);
		}
	}
}
?>